package com.android.shop.ui;

import java.util.List;

import com.android.shop.R;
import com.android.shop.adapter.OrderDetailsAdapter;
import com.android.shop.api.MyHttpClient;
import com.android.shop.bean.OrderDetailsBean;
import com.android.shop.bean.Order;
import com.android.shop.constant.MyConstantID;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class OrderDetailActivity extends BaseActivity {
	private List<OrderDetailsBean> listDetails;
	private String address;
	private String orderType;// 1->外卖2->订餐
	private String phoneNum;
	private String state;
	private String orderId;
	private String numOfPeople;

	private ListView listViewForDish;
	private TextView titleText, phoneNumText, addressText,numPeople;
	private RelativeLayout fourthPart;
	private LinearLayout detail_2,detail_3;

	MyHttpClient myHttpClient;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_details);
		// 初始化数据
		initData();
		OrderDetailsAdapter adapter = new OrderDetailsAdapter(listDetails,
				OrderDetailActivity.this);
		listViewForDish.setAdapter(adapter);
	}

	private void initData() {
		listDetails = MyConstantID.listDetails;
		Bundle bundle = getIntent().getExtras();
		address = bundle.getString("address");
		orderType = bundle.getString("orderType");
		phoneNum = bundle.getString("phoneNum");
		state = bundle.getString("state");
		numOfPeople=bundle.getString("numOfPeople");
		orderId = bundle.getString("orderid");

		listViewForDish = (ListView) findViewById(R.id.listViewForOrderDetails);
		titleText = (TextView) findViewById(R.id.title_text);
		phoneNumText = (TextView) findViewById(R.id.phoneDetails);
		addressText = (TextView) findViewById(R.id.addressDetails);
		fourthPart = (RelativeLayout) findViewById(R.id.fourthPart);
		numPeople=(TextView)findViewById(R.id.numPeople);
		detail_2=(LinearLayout)findViewById(R.id.detail_2);
		detail_3=(LinearLayout)findViewById(R.id.detail_3);
		myHttpClient = MyHttpClient.getSingle();

		phoneNumText.setText(phoneNum);
		addressText.setText(address);
		numPeople.setText(numOfPeople);
		if (orderType.equals("1")) {
			titleText.setText("外卖订单详情");
			detail_3.setVisibility(View.GONE);
		} else {
			titleText.setText("点餐订单详情");
			detail_2.setVisibility(View.GONE);
		}

		if (state.equals(MyConstantID.UNTREATED)) {
			fourthPart.setVisibility(View.VISIBLE);
		} else {
			fourthPart.setVisibility(View.GONE);
		}
	}

	public void login_back(View v) {
		MyConstantID.listDetails.removeAll(listDetails);
		this.finish();
	}

	// 确认处理
	public void handleOrder(View v) {
		new Thread(new SubmitOrderThread()).start();
	}

	// 确认不处理
	public void cancelHandleOrder(View v) {
		new AlertDialog.Builder(this).setTitle("请填写不处理的原因:")
		.setIcon(android.R.drawable.ic_dialog_info)
		.setView(new EditText(this)).setPositiveButton("确定",new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				new Thread(new SubmitCancelOrderThread()).start();
			}
		})
		.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss(); //关闭alertDialog
			}
		}).show();
	}
	
	String result;
	class SubmitOrderThread implements Runnable {
		@Override
		public void run() {
			result = myHttpClient.updateOrder(orderId, "confirm_suc",
					"seller");
			handlerOrder.sendEmptyMessage(1);
		}
	}
	Handler handlerOrder=new Handler(){
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {
				if (result.equals("SUC")) {
					Toast.makeText(getBaseContext(), "处理成功", Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getBaseContext(), "网络异常,请稍后重试", Toast.LENGTH_SHORT)
							.show();
				}
			}
		}
	};
	
	class SubmitCancelOrderThread implements Runnable {
		@Override
		public void run() {
			result = myHttpClient.updateOrder(orderId, "confirm_fail","seller");		
			handlerCancelOrder.sendEmptyMessage(1);
		}
	}
	Handler handlerCancelOrder=new Handler(){
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {
				if (result.equals("SUC")) {
					Toast.makeText(getBaseContext(), "处理成功", Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getBaseContext(), "网络异常,请稍后重试", Toast.LENGTH_SHORT)
							.show();
				}
			}
		}
	};
	
}
